package com.dao;

import com.entity.CheckItem;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description 检查项目==>实现类
 * @Author hyl
 * @Date 2022/4/18 13:45
 **/
public class CheckItemDaoImpl extends BaseDao implements CheckItemDao {

	@Override
	public void findAll() {
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="Select checkItemId,checkItemName,checkItemCost from checkItem";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			//执行
			rs = psm.executeQuery();//查询==》返回的是，虚拟表==》sql的运行结果
			//处理返回结果
			while (rs.next()){
				Integer checkItemId = rs.getInt(1);
				String checkItemName = rs.getString("checkItemName");
				Double	checkItemCost = rs.getDouble("checkItemCost");
				System.out.println(checkItemId + " "+ checkItemName+" "+checkItemCost);
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(conn,psm,rs);
		}
	}

	@Override
	public List<CheckItem> find() {
		List<CheckItem> list = new ArrayList<CheckItem>();
		//创建链接
		Connection conn = getConnection();
		//编写sql
		String sql="Select checkItemId,checkItemName,checkItemCost from checkItem";
		//创建执行对象
		PreparedStatement psm = null;
		ResultSet rs = null;
		try {
			psm = conn.prepareStatement(sql);
			//执行
			rs = psm.executeQuery();//查询==》返回的是，虚拟表==》sql的运行结果
			//处理返回结果
			while (rs.next()){
				//将从数据库中，获取的数据==》添加到集合中
				CheckItem checkItem = new CheckItem(
						rs.getInt("checkItemId"),
						rs.getString("checkItemName"),
						rs.getDouble("checkItemCost"));
				list.add(checkItem);
			}
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(conn,psm,rs);
		}
		return list;
	}

	@Override
	public CheckItem findById(Integer id) {
		return null;
	}

	@Override
	public int insert(CheckItem item) {
		Connection conn = getConnection();
		PreparedStatement pstm = null;
		try {
			//3、编写sql
			String sql = "insert into checkItem(checkItemName,checkItemCost) values(?,?)";
			//4、创建执行对象
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, item.getCheckItemName());
			pstm.setDouble(2, item.getCheckItemCost());
			//5、执行
			int ret =	pstm.executeUpdate();//增、删、改
			//6、处理返回结果
//			if(ret > 0){
//				System.out.println("添加成功");
//			}
			return ret;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(conn, pstm,null);
		}
		return 0;
	}

	@Override
	public int update(CheckItem item) {
		Connection conn = getConnection();
		PreparedStatement pstm = null;
		try {
			//3、编写sql
			String sql = "update checkItem set checkItemName=?,checkItemCost=? where checkItemId = ?";
			//4、创建执行对象
			pstm = conn.prepareStatement(sql);
			pstm.setString(1, item.getCheckItemName());
			pstm.setDouble(2, item.getCheckItemCost());
			pstm.setInt(3, item.getCheckItemId());
			//5、执行
			int ret =	pstm.executeUpdate();//增、删、改
			//6、处理返回结果
			return ret;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(conn, pstm,null);
		}
		return 0;
	}

	@Override
	public int delete(Integer id) {
		Connection conn = getConnection();
		PreparedStatement pstm = null;
		//2、创建链接对象
		try {

			//3、编写sql
			String sql = "delete  from checkItem where checkItemId = ?";
			//4、创建执行对象
			pstm = conn.prepareStatement(sql);
			pstm.setInt(1, id);
			//5、执行
			int ret =	pstm.executeUpdate();//增、删、改
			//6、处理返回结果
			return ret;
		} catch (SQLException throwables) {
			throwables.printStackTrace();
		}finally {
			closeAll(conn, pstm, null);
		}
		return 0;
	}
}




